package org.agile4j.plugin.gun.utils;

import java.util.List;

import org.agile4j.plugin.gun.model.FieldEntity;
import org.agile4j.plugin.gun.model.ReplaceEntity;
import org.apache.commons.lang.StringUtils;

public final class ReplaceUtil {

	private static final String SPLIT = "_" ;
	
	public static void replaceExcel(List<ReplaceEntity> replaceEntitylist, List<FieldEntity> fieldEntitylist) {
		for (FieldEntity fieldEntity : fieldEntitylist) {
			for (ReplaceEntity replaceEntity : replaceEntitylist) {
 				if (StringUtils.isNotEmpty(replaceEntity.getSubName())) {
//					if (fieldEntity.getNewFieldName().contains(replaceEntity.getCnName())) {
						String subFiel = fieldEntity.getNewFieldName() ;
						String repalceStr = StringUtils.replace(subFiel, replaceEntity.getCnName(), SPLIT+replaceEntity.getSubName()+SPLIT) ;
						repalceStr = StringUtils.replace(repalceStr, SPLIT + SPLIT, SPLIT) ;
						if (SPLIT.equals(StringUtils.left(repalceStr, 1))) {
							repalceStr = StringUtils.substring(repalceStr, 1, repalceStr.length()) ;
						}
						if (SPLIT.equals(StringUtils.right(repalceStr, 1))) {
							repalceStr = StringUtils.substring(repalceStr, 0, repalceStr.length()-1) ;
						}
						fieldEntity.setNewFieldName(repalceStr);
//					}
				}
			}
		}
//		ExcelHelper.saveReplaceData(fieldEntitylist, "E:\\new_replace_data.xlsx");
	}
	
}
